package com.sunlgggg.view.component;

import com.google.gson.Gson;
import com.google.gson.internal.LinkedTreeMap;
import com.sunlgggg.object.StudentInfo;
import com.sunlgggg.util.HttpRequestUtils;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;

/**
 * @author sunlggggg
 * @date 2016/12/1
 */
public class StudentInfoTable extends JTable {


    String[] columnNames = {"学号", "姓名", "性别", "出生日期", "语文", "数学", "英语"};
    DefaultTableModel tableModel;

    public StudentInfoTable() {
        tableModel = new DefaultTableModel(new String[][]{}, columnNames);
        this.setModel(tableModel);
    }

    private void addStudentInfo(StudentInfo studentInfo) {
        tableModel.addRow(studentInfo.getStrings());
    }


    public void load(String info){
        tableModel.setRowCount(0);
        java.util.List infos = new Gson().fromJson(info, java.util.List.class);
        if (infos != null && infos.size() > 0) {
            for (int i = 0; i < infos.size(); i++) {
                System.out.println(new StudentInfo((LinkedTreeMap) infos.get(i)));
                this.addStudentInfo(new StudentInfo((LinkedTreeMap) infos.get(i)));
            }
        }
    }

    public void load(){
        tableModel.getDataVector().clear();   //清除表格数据
        tableModel.fireTableDataChanged();//通知模型更新
        this.updateUI();//刷新表格
        java.util.List infos = new Gson().fromJson(HttpRequestUtils.get("info/allInfo"), java.util.List.class);
        if (infos != null && infos.size() > 0) {
            for (int i = 0; i < infos.size(); i++) {
                System.out.println(new StudentInfo((LinkedTreeMap) infos.get(i)));
                this.addStudentInfo(new StudentInfo((LinkedTreeMap) infos.get(i)));
            }
        }
    }

    public static void main(String[] args) {
        new StudentInfoTable();
    }


}